/*
 * DILARANG MENGHAPUS ATAU MENGEDIT COPYRIGHT INI.
 * 
 * Copyright 2008 echo.khannedy@gmail.com. 
 * All rights reserved.
 * 
 * Semua isi dalam file ini adalah hak milik dari echo.khannedy@gmail.com
 * Anda tak diperkenankan untuk menggunakan file atau mengubah file
 * ini kecuali anda tidak menghapus atau merubah lisence ini.
 * 
 * File ini dibuat menggunakan :
 * IDE        : NetBeans
 * NoteBook   : Acer Aspire 5920G
 * OS         : Windows Vista
 * Java       : Java 1.6
 * 
 */
package usu.perpustakaan.buku.widget.admin;

import java.awt.Component;
import java.sql.Connection;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableRowSorter;
import usu.perpustakaan.buku.widget.template.TableModelOperator;
import usu.perpustakaan.buku.widget.template.TextBoxGlass;
import usu.widget.ButtonGlass;
import usu.widget.Panel;
import usu.widget.util.WidgetUtilities;

/**
 *
 * @author  usu
 */
public class ManageOperator extends Panel implements DocumentListener {

    private static final long serialVersionUID = -1;
    private TableModelOperator modelTable;

    /** Creates new form BeanForm */
    public ManageOperator() {
        initVariables();
        initComponents();
        initFinals();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usu.perpustakaan.buku.widget.template.ViewPortGlass viewPort = new usu.perpustakaan.buku.widget.template.ViewPortGlass();
        table = new javax.swing.JTable();
        javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane();
        textSearch = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        buttonDelete = new usu.widget.ButtonGlass();
        buttonEdit = new usu.widget.ButtonGlass();
        buttonAdd = new usu.widget.ButtonGlass();
        buttonRefresh = new usu.widget.ButtonGlass();

        viewPort.setView(table);

        table.setAutoCreateRowSorter(true);
        table.setForeground(new java.awt.Color(255, 255, 255));
        table.setToolTipText("Tabel operator");
        table.setFillsViewportHeight(true);
        table.setOpaque(false);
        table.setSelectionBackground(new java.awt.Color(51, 255, 52));
        table.setSelectionForeground(new java.awt.Color(0, 0, 0));

        setOpaque(false);
        setOpaqueGradient(false);
        setOpaqueImage(false);

        scrollPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        scrollPane.setOpaque(false);
        scrollPane.setViewport(viewPort);

        textSearch.setForeground(new java.awt.Color(255, 255, 255));
        textSearch.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textSearch.setToolTipText("Masukkan kata yang akan dicari");
        textSearch.setFocusAccelerator('C');
        textSearch.setFont(new java.awt.Font("Tahoma", 1, 11));

        jLabel1.setDisplayedMnemonic('C');
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Cari (case sensitive) ");

        buttonDelete.setMnemonic('H');
        buttonDelete.setText("Hapus");
        buttonDelete.setToolTipText("Klik untuk menghapus operator");
        buttonDelete.setRoundRect(true);

        buttonEdit.setMnemonic('U');
        buttonEdit.setText("Ubah");
        buttonEdit.setToolTipText("Klik untuk mengubah operator");
        buttonEdit.setRoundRect(true);

        buttonAdd.setMnemonic('A');
        buttonAdd.setText("Tambah");
        buttonAdd.setToolTipText("Klik untuk menambah operator");
        buttonAdd.setRoundRect(true);

        buttonRefresh.setMnemonic('S');
        buttonRefresh.setText("Segarkan");
        buttonRefresh.setToolTipText("Klik untuk meload ulang database");
        buttonRefresh.setRoundRect(true);
        buttonRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 317, Short.MAX_VALUE)
                        .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void buttonRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRefreshActionPerformed
// TODO add your handling code here:
    textSearch.setText("");
    modelTable.removeAllElements();
}//GEN-LAST:event_buttonRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.widget.ButtonGlass buttonAdd;
    usu.widget.ButtonGlass buttonDelete;
    usu.widget.ButtonGlass buttonEdit;
    usu.widget.ButtonGlass buttonRefresh;
    javax.swing.JTable table;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textSearch;
    // End of variables declaration//GEN-END:variables

    @Override
    @SuppressWarnings("unchecked")
    public void insertUpdate(DocumentEvent e) {
        if (table.getRowSorter() != null) {
            if (textSearch.getText().equals("")) {
                ((TableRowSorter<TableModelOperator>) table.getRowSorter()).setRowFilter(null);
            } else {
                ((TableRowSorter<TableModelOperator>) table.getRowSorter()).setRowFilter(
                        RowFilter.regexFilter(textSearch.getText()));
            }
        }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        insertUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        insertUpdate(e);
    }

    /**
     * button refresh
     * @return
     */
    public ButtonGlass getButtonRefresh() {
        return buttonRefresh;
    }

    /**
     * deklarasi akhir
     */
    @SuppressWarnings("unchecked")
    private void initFinals() {

        textSearch.getDocument().addDocumentListener(this);

        table.setModel(modelTable);
        table.getTableHeader().setReorderingAllowed(false);
        WidgetUtilities.setAutomaticPopUpMenu(table);
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new RenderTable());
        }
    }

    /**
     * deklarasi variabel
     */
    private void initVariables() {
        modelTable = new TableModelOperator();
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonAdd() {
        return buttonAdd;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonDelete() {
        return buttonDelete;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonEdit() {
        return buttonEdit;
    }

    /**
     * 
     * @return
     */
    public TableModelOperator getModelTable() {
        return modelTable;
    }

    /**
     * 
     * @return
     */
    public JTable getTable() {
        return table;
    }

    /**
     * 
     * @return
     */
    public TextBoxGlass getTextSearch() {
        return textSearch;
    }

    /**
     * 
     */
    public static class RenderTable extends DefaultTableCellRenderer {

        private static final long serialVersionUID = -1;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value != null) {
                setToolTipText(value.toString());
                setOpaque(isSelected);
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            } else {
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
        }
    }
}
